package GUILayer;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import ControlLayer.ProductCtr;
import ModelLayer.Product;

import javax.swing.JTable;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JTextArea;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

public class ProductGUI extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ProductCtr ctr;

	private JTextField searchField;
	private JTable productTable;
	private DefaultTableModel productTableModel;
	private JPanel productListPanel;
	private JScrollPane productScrollPane;

	private JButton btnNew;
	private JButton btnEdit;
	private JButton btnSave;
	private JButton btnDelete;
	private JButton btnCC;

	private int supplierTempID = 1;

	private boolean isSelected = false;
	private boolean isEditing = false;
	private boolean isNewEntry = false;
	private boolean isClean = false;
	private JTextField txt_ProductID;
	private JTextField txt_ProductName;
	private JTextField txt_PayPrice;
	private JTextField txt_SalePrice;
	private JTextField txt_rentPrice;
	private JTextField txt_CoO;
	private JTextField txt_Color;
	private JTextField txt_Size;
	private JTextField txt_Calibre;
	private JTextField txt_fabric;
	private JTextField txt_minStock;
	private JTextField txt_currentStock;
	private JCheckBox chckbxLowOnStock;
	private JTextField txt_eqType;
	private JTextArea txt_Description;

	public ProductGUI() {
		ctr = new ProductCtr();
		initialize();
	}

	/**
	 * Create the panel.
	 */
	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		productListPanel = new JPanel();
		add(productListPanel, BorderLayout.WEST);
		productListPanel.setLayout(new BorderLayout(0, 0));

		JPanel productInfoPanel = new JPanel();
		add(productInfoPanel, BorderLayout.CENTER);
		productInfoPanel.setLayout(new BorderLayout(0, 0));

		JPanel productPanel = new JPanel();
		productInfoPanel.add(productPanel, BorderLayout.CENTER);
		productPanel.setLayout(new BorderLayout(0, 0));

		JPanel searchPanel = new JPanel();
		productListPanel.add(searchPanel, BorderLayout.NORTH);
		searchPanel.setLayout(new BorderLayout(0, 0));

		searchField = new JTextField();
		searchPanel.add(searchField);
		searchField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				populateTable();
			}
		});
		searchField.setColumns(10);

		JPanel productGenericInfoPanel = new JPanel();
		productGenericInfoPanel.setBorder(new TitledBorder(null,
				"Product Info", TitledBorder.LEADING, TitledBorder.TOP, null,
				null));
		productGenericInfoPanel.setToolTipText("Product Information");
		productPanel.add(productGenericInfoPanel, BorderLayout.CENTER);

		txt_ProductID = new JTextField();
		txt_ProductID.setEditable(false);
		txt_ProductID.setColumns(10);

		JLabel lblNewLabel = new JLabel("Product ID");

		JLabel lblName = new JLabel("Name");

		txt_ProductName = new JTextField();
		txt_ProductName.setEditable(false);
		txt_ProductName.setColumns(10);

		txt_PayPrice = new JTextField();
		txt_PayPrice.setEditable(false);
		txt_PayPrice.setColumns(10);

		txt_SalePrice = new JTextField();
		txt_SalePrice.setEditable(false);
		txt_SalePrice.setColumns(10);

		txt_rentPrice = new JTextField();
		txt_rentPrice.setEditable(false);
		txt_rentPrice.setColumns(10);

		JLabel lblPayPrice = new JLabel("Pay price");

		JLabel lblSalePrice = new JLabel("Sale price");

		JLabel lblRentPrice = new JLabel("Rent price");

		JLabel lblEqType = new JLabel("Eq. type");

		JLabel lblCountryOfOrigin = new JLabel("Country of Origin");

		JLabel lblDesc = new JLabel("Description");

		txt_CoO = new JTextField();
		txt_CoO.setEditable(false);
		txt_CoO.setColumns(10);

		JLabel lblColor = new JLabel("Color");

		JLabel lblSize = new JLabel("Size");

		JLabel lblCalibre = new JLabel("Calibre");

		JLabel lblFabric = new JLabel("Fabric");

		txt_Color = new JTextField();
		txt_Color.setEditable(false);
		txt_Color.setColumns(10);

		txt_Size = new JTextField();
		txt_Size.setEditable(false);
		txt_Size.setColumns(10);

		txt_Calibre = new JTextField();
		txt_Calibre.setEditable(false);
		txt_Calibre.setColumns(10);

		txt_fabric = new JTextField();
		txt_fabric.setEditable(false);
		txt_fabric.setColumns(10);

		txt_Description = new JTextArea();
		txt_Description.setEnabled(false);

		txt_eqType = new JTextField();
		txt_eqType.setEditable(false);
		txt_eqType.setColumns(10);
		GroupLayout gl_productGenericInfoPanel = new GroupLayout(
				productGenericInfoPanel);
		gl_productGenericInfoPanel.setHorizontalGroup(
			gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
							.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblName)
								.addComponent(lblEqType)
								.addComponent(lblRentPrice)
								.addComponent(lblSalePrice)
								.addComponent(lblPayPrice)
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
									.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING, false)
											.addComponent(txt_PayPrice)
											.addComponent(txt_SalePrice)
											.addComponent(txt_rentPrice))
										.addComponent(txt_eqType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addGap(24)
									.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.TRAILING)
										.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
											.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING)
												.addComponent(lblColor)
												.addComponent(lblSize))
											.addGap(27)
											.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.TRAILING)
												.addComponent(txt_Size, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
												.addComponent(txt_Color, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
										.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.TRAILING, false)
											.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
												.addComponent(lblFabric)
												.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(txt_fabric, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
											.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
												.addComponent(lblCalibre)
												.addGap(18)
												.addComponent(txt_Calibre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
									.addGap(64))
								.addComponent(txt_ProductName, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
								.addComponent(txt_ProductID, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)))
						.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
							.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblCountryOfOrigin)
								.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
									.addGap(4)
									.addComponent(lblDesc)))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(txt_Description, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
								.addComponent(txt_CoO, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))))
					.addContainerGap())
		);
		gl_productGenericInfoPanel.setVerticalGroup(
			gl_productGenericInfoPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_productGenericInfoPanel.createSequentialGroup()
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 12, GroupLayout.PREFERRED_SIZE)
						.addComponent(txt_ProductID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblName)
						.addComponent(txt_ProductName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPayPrice)
						.addComponent(txt_Color, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblColor)
						.addComponent(txt_PayPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSalePrice)
						.addComponent(txt_Size, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblSize)
						.addComponent(txt_SalePrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblRentPrice)
						.addComponent(txt_Calibre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblCalibre)
						.addComponent(txt_rentPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEqType)
						.addComponent(txt_fabric, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblFabric)
						.addComponent(txt_eqType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCountryOfOrigin)
						.addComponent(txt_CoO, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_productGenericInfoPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDesc)
						.addComponent(txt_Description))
					.addGap(152))
		);
		productGenericInfoPanel.setLayout(gl_productGenericInfoPanel);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Stock information",
				TitledBorder.LEADING, TitledBorder.BOTTOM, null, null));
		productPanel.add(panel, BorderLayout.SOUTH);

		txt_currentStock = new JTextField();
		txt_currentStock.setEditable(false);
		txt_currentStock.setColumns(10);

		JLabel lblCurrentStock = new JLabel("Current stock:");

		JLabel lblMinStock = new JLabel("Min. stock");

		txt_minStock = new JTextField();
		txt_minStock.setEditable(false);
		txt_minStock.setColumns(10);

		chckbxLowOnStock = new JCheckBox("Low on stock");
		chckbxLowOnStock.setEnabled(false);

		JButton btnSupplier = new JButton("Supplier");
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addComponent(lblCurrentStock)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addGroup(
								gl_panel.createParallelGroup(
										Alignment.TRAILING, false)
										.addComponent(chckbxLowOnStock,
												Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(txt_currentStock,
												Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 96,
												Short.MAX_VALUE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(lblMinStock, GroupLayout.DEFAULT_SIZE,
								GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addGroup(
								gl_panel.createParallelGroup(Alignment.LEADING,
										false)
										.addComponent(btnSupplier,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(txt_minStock,
												GroupLayout.DEFAULT_SIZE, 95,
												Short.MAX_VALUE))
						.addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(lblCurrentStock)
												.addComponent(
														txt_currentStock,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(lblMinStock)
												.addComponent(
														txt_minStock,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(chckbxLowOnStock)
												.addComponent(btnSupplier))
								.addContainerGap(GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		panel.setLayout(gl_panel);

		JPanel menuPanel = new JPanel();
		productInfoPanel.add(menuPanel, BorderLayout.SOUTH);
		menuPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		btnNew = new JButton("Create");
		btnNew.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				isSelected = false;
				isEditing = false;
				isNewEntry = true;
				isClean = false;
				enableButtons();
				setFields(true);
				clearFields();
				txt_ProductID.setText(ctr.getNextID() + "");
			}
		});
		menuPanel.add(btnNew);

		btnEdit = new JButton("Edit");
		btnEdit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				isSelected = false;
				isEditing = true;
				isNewEntry = false;
				isClean = false;
				setFields(true);
				enableButtons();
			}
		});
		btnEdit.setEnabled(false);
		menuPanel.add(btnEdit);

		btnSave = new JButton("Save");
		btnSave.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (isEditing) {
					try {
						ctr.updateProduct(
								Integer.parseInt(txt_ProductID.getText()),
								txt_ProductName.getText(),
								Double.parseDouble(txt_PayPrice.getText()),
								Double.parseDouble(txt_SalePrice.getText()),
								Double.parseDouble(txt_rentPrice.getText()),
								txt_CoO.getText(),
								Integer.parseInt(txt_currentStock.getText()),
								Integer.parseInt(txt_minStock.getText()),
								chckbxLowOnStock.isSelected(),
								Integer.parseInt(txt_Size.getText()),
								supplierTempID, txt_Color.getText(),
								txt_eqType.getText(),
								txt_Description.getText(),
								txt_fabric.getText(), txt_Calibre.getText());
					} catch (Exception e) {
						JOptionPane.showMessageDialog(
								null,
								"Please input correct values!\n"
										+ e.getMessage(), "Error",
								JOptionPane.ERROR_MESSAGE);
					}
				} else if (isNewEntry) {
					try {
						ctr.insertProduct(txt_ProductName.getText(),
								Double.parseDouble(txt_PayPrice.getText()),
								Double.parseDouble(txt_SalePrice.getText()),
								Double.parseDouble(txt_rentPrice.getText()),
								txt_CoO.getText(),
								Integer.parseInt(txt_currentStock.getText()),
								Integer.parseInt(txt_minStock.getText()),
								chckbxLowOnStock.isSelected(),
								Integer.parseInt(txt_Size.getText()),
								supplierTempID, txt_Color.getText(),
								txt_eqType.getText(),
								txt_Description.getText(),
								txt_fabric.getText(), txt_Calibre.getText());
					} catch (Exception e) {
						JOptionPane.showMessageDialog(
								null,
								"Please input correct values!\n"
										+ e.getMessage(), "Error",
								JOptionPane.ERROR_MESSAGE);
					}
				} else {

				}

				isSelected = false;
				isEditing = false;
				isNewEntry = false;
				isClean = true;
				setFields(false);
				enableButtons();
				clearFields();
				populateTable();
			}
		});
		btnSave.setEnabled(false);
		menuPanel.add(btnSave);

		btnDelete = new JButton("Delete");
		btnDelete.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Product p = ctr.getProduct(Integer.parseInt(txt_ProductID
						.getText()));
				int response = JOptionPane.showConfirmDialog(
						null,
						"This will delete product #"
								+ p.getProductId()
								+ " from the database.\n Do you wish to continue?",
						"Confirm delete", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				switch (response) {
				case JOptionPane.NO_OPTION:
					break;
				case JOptionPane.YES_OPTION:
					ctr.removeProduct(p.getProductId());
					setFields(false);
					isSelected = false;
					isEditing = false;
					isNewEntry = false;
					isClean = true;
					enableButtons();
					clearFields();
					populateTable();
					break;
				}
			}
		});
		btnDelete.setEnabled(false);
		menuPanel.add(btnDelete);

		btnCC = new JButton("Cancel/Clear");
		btnCC.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setFields(false);
				isSelected = false;
				isEditing = false;
				isNewEntry = false;
				isClean = true;
				enableButtons();
				clearFields();
				populateTable();
			}
		});
		btnCC.setEnabled(false);
		menuPanel.add(btnCC);

		createTable();
		populateTable();
	}

	private void populateTable() {
		ArrayList<Product> products = ctr.getAllProduct();
		productTableModel.setRowCount(0);

		String searchFilter = searchField.getText();
		System.out.println("ProductGUI - Filtering for \"" + searchFilter
				+ "\"");

		for (Product p : products) {
			if (((Integer) p.getProductId()).toString().toLowerCase()
					.contains(searchFilter.toLowerCase())
					|| p.getName().toLowerCase()
							.contains(searchFilter.toLowerCase())) {
				try {
					productTableModel.addRow(new Object[] { p.getProductId(),
							p.getName(), p.getEquipmentType(),
							p.getCurrentStock() });
				} catch (Exception e) {
				}
			}
		}
	}

	private void createTable() {
		productTableModel = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		productTableModel.addColumn("ID");
		productTableModel.addColumn("Name");
		productTableModel.addColumn("Product Type");
		productTableModel.addColumn("Stock");

		productScrollPane = new JScrollPane();
		productListPanel.add(productScrollPane, BorderLayout.CENTER);

		productTable = new JTable(productTableModel);
		productTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		productTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
		productTable.setAutoCreateRowSorter(true);

		productTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				setFields(false);
				updateProductInfo();
				isSelected = true;
				isEditing = false;
				isNewEntry = false;
				isClean = false;
				enableButtons();
			}
		});

		productScrollPane.setViewportView(productTable);
	}

	private void updateProductInfo() {
		Product p = ctr.getProduct((Integer) productTable.getValueAt(
				productTable.getSelectedRow(), 0));
		txt_ProductID.setText(p.getProductId() + "");
		txt_ProductName.setText(p.getName());
		txt_PayPrice.setText(p.getPayPrice() + "");
		txt_SalePrice.setText(p.getSalePrice() + "");
		txt_rentPrice.setText(p.getRentPrice() + "");
		txt_CoO.setText(p.getCountryOfOrigin());
		txt_Color.setText(p.getColor());
		txt_Size.setText(p.getClothSize() + "");
		txt_Calibre.setText(p.getCalibre());
		txt_fabric.setText(p.getFabric());
		txt_minStock.setText(p.getMinStock() + "");
		txt_currentStock.setText(p.getCurrentStock() + "");
		txt_eqType.setText(p.getEquipmentType());
		chckbxLowOnStock.setSelected(p.isBelowMin());
	}

	private void enableButtons() {
		if (isSelected) {
			btnEdit.setEnabled(true);
			btnDelete.setEnabled(true);
			btnNew.setEnabled(false);
			btnSave.setEnabled(false);
			btnCC.setEnabled(true);
		}
		if (isNewEntry) {
			btnDelete.setEnabled(false);
			btnEdit.setEnabled(false);
			btnNew.setEnabled(false);
			btnSave.setEnabled(true);
			btnCC.setEnabled(true);
		}
		if (isEditing) {
			btnDelete.setEnabled(true);
			btnEdit.setEnabled(false);
			btnNew.setEnabled(false);
			btnSave.setEnabled(true);
			btnCC.setEnabled(true);
		}
		if (isClean) {
			btnDelete.setEnabled(false);
			btnEdit.setEnabled(false);
			btnNew.setEnabled(true);
			btnSave.setEnabled(false);
			btnCC.setEnabled(false);
		}
	}

	private void setFields(boolean val) {
		txt_ProductName.setEditable(val);
		txt_currentStock.setEditable(val);
		txt_PayPrice.setEditable(val);
		txt_SalePrice.setEditable(val);
		txt_rentPrice.setEditable(val);
		txt_CoO.setEditable(val);
		txt_Color.setEditable(val);
		txt_Size.setEditable(val);
		txt_Calibre.setEditable(val);
		txt_fabric.setEditable(val);
		txt_minStock.setEditable(val);
		txt_Description.setEditable(val);
		txt_eqType.setEditable(val);
	}

	private void clearFields() {
		txt_ProductID.setText("");
		txt_ProductName.setText("");
		txt_PayPrice.setText("");
		txt_SalePrice.setText("");
		txt_rentPrice.setText("");
		txt_CoO.setText("");
		txt_Color.setText("");
		txt_Size.setText("");
		txt_Calibre.setText("");
		txt_Description.setText("");
		txt_fabric.setText("");
		txt_currentStock.setText("");
		txt_minStock.setText("");
		txt_eqType.setText("");
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}
}
